<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;"
        @click="changeFn('weight_desc')"
        :class="{ active: params.sorter === 'weight_desc' }"
        >推荐</a
      >
      <a
        href="javascript:;"
        @click="changeFn('')"
        :class="{ active: params.sorter === '' }"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <van-list
      v-model="loading"
      :finished="finished"
      :immediate-check="false"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArticleItem
        v-for="item in list"
        :key="item.id"
        :article="item"
      ></ArticleItem>
    </van-list>
  </div>
</template>

<script>
import { getArticleList } from "@/api/article";
export default {
  name: "ArticlePage",
  data() {
    return {
      params: {
        current: 1,
        pageSize: 10,
        sorter: "weight_desc",
      },
      list: [],
      loading: false,
      finished: false,
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      try {
        const {
          data: { rows, total },
        } = await getArticleList(this.params);
        // console.log(rows);
        this.list = [...this.list, ...rows];
        this.total = total;
      } catch (err) {
        console.log(err);
      }
    },
    //? 实现滚动数据让数据分批加载出来
    async onLoad() {
      //1. 首先让 this.params.current++,才可以拿到下一页的数据
      this.params.current++;
      //2. 然后再次象后端发送请求，获取第二页的数据,调用写好的方法即可
      await this.getList(); //用 await等待，获取数据完成，才会执行后面的
      //3. 拿到数据后，让 loading 变成 false(目的是通知加载状态结束，才会再次触发onLoad事件)
      this.loading = false;
      //4. 判断一下后台的数据的总数是否大于list的长度，如果大于就停止加载
      if (this.list.length >= this.total) {
        this.finished = true;
      }
    },
    //? 实现推荐和最新的功能切换
    changeFn(val) {
      // 实现列表的切换，实际上就是判断发送请求的时候 params.sorter 参数携带的值
      // 点击两个链接的时候，将不同的字段传给 params.sorter
      this.params.sorter = val;

      // 初始化数据
      this.list = []; //初始化数据列表
      this.finished = false; //如果在其中一个列表内一直加载到数据没有了，就会把finished变成true，所以需要初始化
      this.loading = false; // 初始化 loading
      this.params.current = 1; //初始化发送请求是携带的 params.current 的字段

      // 重新发送请求
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: "";
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>
